<template>
  <div>
    <div id="particles-js"></div>
    <el-card shadow="always" class="login-ruleForm">
      <el-divider content-position="right"
        ><h2 style="color: #b8b9bb">登录系统</h2></el-divider
      >
      <br />
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="60px"
      >
        <el-form-item label="账户：" prop="username">
          <el-input v-model.number="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            style="margin-left: 80px"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>    
<script>
import 'element-ui/lib/theme-chalk/index.css'
// import particles from 'particles.js';
import * as particlesJs from 'particles.js'
import particlesConfig from '../assets/particles.json'
import service from '../api/request'
import axios from 'axios'
import { login } from '../api/login_api'
import GLOBAL_FUN from '../api/global_function.js'

export default {
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账户'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    return {
      ruleForm: {
        pass: '',
        checkPass: '',
        username: ''
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        username: [{ validator: validateUsername, trigger: 'blur' }]
      }
    }
  },
  mounted: function () {
    particlesJS('particles-js', particlesConfig, function () {
      console.log('callback - particles.js config loaded')
    })
    // particlesJS("particles-js", particlesConfig);
  },
  methods: {
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login({
            userId: this.ruleForm.username,
            password: this.ruleForm.pass
          }).then((r) => {
            if (r.code === '200') {
              _this.GLOBAL_FUN.successMsg()
              GLOBAL_FUN.setToken(r.result)
              _this.$router.push({ path: '/', query: {} })
            } else {
                _this.GLOBAL_FUN.errorMsg(r.message);
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
  
<style scoped>
.login-ruleForm {
  padding: 10px 0 0 0;
  z-index: 10;
  position: absolute;
  width: 430px;
  margin-top: 20%;
  margin-left: 65%;
}

#particles-js {
  z-index: -1;
  position: absolute;
  width: 99.5%;
  height: 98.5%;
  background-color: #2d3a4b;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 50% 50%;
}

.el-card {
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  transition: 0.3s;
}
</style>